﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCom;
using System.Data;
using System.Xml;
using XmlDataCommunication.Properties;

namespace XmlDataCommunication
{
    public class XmlLineWatchSysDataAccess : ILineWatchSysAccess
    {
        public DataRow GetRecordByLineCodeAndShift(string lineCode, string shift)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNode node = doc.SelectSingleNode("//Line_WatchSys/Record[Line_Code='" + lineCode + "' and Function='" + shift + "']");
            if (node != null) return GeneralFunctions.GetDataRowByXmlNode(node);
            else return null;
        }


        public DataTable GetRecordsByLineCode(string lineCode)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNodeList nodes = doc.SelectNodes("//Line_WatchSys/Record[Line_Code='" + lineCode + "']");
            if (nodes != null) return GeneralFunctions.GetDataTableByXmlNodes(nodes);
            else return null;
        }


        public void UpdateRecordByLineAndFunction(string line, string function, string content1, string content2, string content3, string content4, string user)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNode node = doc.SelectSingleNode("//Line_WatchSys/Record[Line_Code='" + line + "' and Function='" + function + "']");
            if (node == null)
            {
                List<string> columnNames = new List<string>()
                {
                    "Line_Code","Function","Content1","Content2","Content3","Content4","SetDate","User"
                };
                node = GeneralFunctions.CreateRecordInTable(doc, "Line_WatchSys", columnNames);
                node.SelectSingleNode("./Line_Code").InnerText = line;
                node.SelectSingleNode("./Function").InnerText = function;
            }
            node.SelectSingleNode("./Content1").InnerText = content1;
            node.SelectSingleNode("./Content2").InnerText = content2;
            node.SelectSingleNode("./Content3").InnerText = content3;
            node.SelectSingleNode("./Content4").InnerText = content4;
            node.SelectSingleNode("./User").InnerText = user;
            node.SelectSingleNode("./SetDate").InnerText = DateTime.Now.Date.ToString("yyyy-MM-dd");
            GeneralFunctions.SaveXmlDocument(doc);
        }
    }
}
